<?php
class controller_sms extends model_sms{

    private function sms_get_args($get = true)
    {
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey', $para, 'S');
        $args['ps'] = $this->existsKeyInArray('ps', $para, 'N', $this->pagesize);
        return $args;
    }

    private function sms_page_args($args)
    {
        $page_args = array();
        if (!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if (!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }

	public function index(){
        $args = $this->sms_get_args(true);
        $page_args = $this->sms_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_sms_list($args);
        $args['page_max'] = $this->get_maxpage($args['ps'], $datas['record']);

        $basepath = '/sms/index';

        $this->display("sms/index", array(
            'basepath' => $basepath,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
	}

    public function index_add(){
        $basepath = '/sms/index';
        $html = '<div id="mainbody">';
        $html .= '<div class="main">';
        $html .= '<div class="info">';
        $html .= '<form name="add_save" action="'.$basepath.'_add_save"  method="POST" target="ajax" func="form_submit">';
        $html .= '<dl><dt>通道名称：</dt><dd><input type="text" name="title" class="wtxt" value="253短信接口" /></dd></dl>';
        $html .= '<dl><dt>登录帐号：</dt><dd><input type="text" name="account" class="wtxt" value="" /></dd></dl>';
        $html .= '<dl><dt>登录密钥：</dt><dd><input type="text" name="passwd" class="wtxt" value="" /></dd></dl>';
        $html .= '<dl><dt>发送API：</dt><dd><input type="text" name="apiurl" class="wtxt" value="http://smssh1.253.com/msg/send/json" /></dd></dl>';
        $html .= '<dl><dt>查询API：</dt><dd><input type="text" name="queryurl" class="wtxt" value="http://smssh1.253.com/msg/balance/json" /></dd></dl>';
        $html .= '<dl><dt>短信内容：</dt><dd><input type="text" name=" smscontent" class="wtxt" value="注册短信验证码为：{verify}，如果没有提交请求请忽略。" /></dd></dl>';
        $html .= '<dl><dt>&nbsp;</dt><dd class="t">验证码的内容请用：{verify} 替代，系统将替换成对应的验证码字符</dd></dl>';
        $html .= '<dl><dt>通道状态：</dt><dd><select name="state"><option value="1">启用</option><option value="0">禁用</option></select></dd></dl>';
        $html .= '</form>';
        $html .= '</div>';
        $html .= '</div>';
        $html .= '</div>';
        $this->dialogue(array(
            'class' => 'mid',
            'body' => $html,
            'yes'  => array(
                'text' => '保存',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }

    public function index_add_save()
    {
        if (!$this->post) core::__403();
        $args = $this->sms_add_edit_args(false);
        unset($args['id']);
        $insertid = $this->table_row_save('@@smsline', $args);
        if (!$insertid) core::error("通道添加失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "通道[ {$args['title']} ]添加成功",
            'auto' => true,
            'yes' => array(
                'text' => '刷新',
                'func' => '$("#btn_refresh").trigger("click");',
            ),
        ));
    }

    public function index_edit(){
        $basepath = '/sms/index';
        $args['id'] = $this->existsKeyInArray('id',$_GET,'N');
        $data = $this->db->getRow('@@smsline','*',$args);
        $html = '<div id="mainbody">';
        $html .= '<div class="main">';
        $html .= '<div class="info">';
        $html .= '<form name="add_save" action="'.$basepath.'_edit_save"  method="POST" target="ajax" func="form_submit">';
        $html .= '<input type="hidden" name="id" class="int" value="'.$data['id'].'" />';
        $html .= '<dl><dt>通道名称：</dt><dd><input type="text" name="title" class="wtxt" value="'.$data['title'].'" /></dd></dl>';
        $html .= '<dl><dt>登录帐号：</dt><dd><input type="text" name="account" class="wtxt" value="'.$data['account'].'" /></dd></dl>';
        $html .= '<dl><dt>登录密钥：</dt><dd><input type="text" name="passwd" class="wtxt" value="'.$data['passwd'].'" /></dd></dl>';
        $html .= '<dl><dt>发送API：</dt><dd><input type="text" name="apiurl" class="wtxt" value="'.$data['apiurl'].'" /></dd></dl>';
        $html .= '<dl><dt>查询API：</dt><dd><input type="text" name="queryurl" class="wtxt" value="'.$data['queryurl'].'" /></dd></dl>';
        $html .= '<dl><dt>短信内容：</dt><dd><input type="text" name=" smscontent" class="wtxt" value="'.$data['smscontent'].'" /></dd></dl>';
        $html .= '<dl><dt>&nbsp;</dt><dd class="t">验证码的内容请用：{verify} 替代，系统将替换成对应的验证码字符</dd></dl>';
        $html .= '<dl><dt>通道状态：</dt><dd><select name="state"><option value="1">启用</option><option value="0">禁用</option></select></dd></dl>';
        $html .= '</form>';
        $html .= '</div>';
        $html .= '</div>';
        $html .= '</div>';
        $this->dialogue(array(
            'class' => 'mid',
            'body' => $html,
            'yes'  => array(
                'text' => '保存',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }

    private function sms_add_edit_args($get = true){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['id'] = $this->existsKeyInArray('id',$para,'N');
        $args['title'] = $this->existsKeyInArray('title',$para,'S');
        $args['account'] = $this->existsKeyInArray('account',$para,'S');
        $args['passwd'] = $this->existsKeyInArray('passwd',$para,'S');
        $args['apiurl'] = $this->existsKeyInArray('apiurl',$para,'S');
        $args['queryurl'] = $this->existsKeyInArray('queryurl',$para,'S');
        $args['smscontent'] = $this->existsKeyInArray('smscontent',$para,'S');
        $args['state'] = $this->existsKeyInArray('state',$para,'N');

        if(empty($args['account'])) core::error('帐号不能为空');
        if(empty($args['passwd'])) core::error('密钥不能为空');
        if(empty($args['apiurl'])) core::error('发送API地址不能为空');
        if(empty($args['smscontent'])) core::error('短信内容不能为空');

        return $args;
    }

    public function index_edit_save()
    {
        $args = $this->sms_add_edit_args(false);
        $id = intval($args['id']);
        unset($args['id']);
        if(empty($id)) core::error('短信通道编号无效');
        $_result = $this->db->update('@@smsline',$args,array('id'=>$id));
        if(!$_result) core::error("编辑失败");
        $this->addLog($this->authid,__FUNCTION__,$_result,$id);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "编辑成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => '$("#btn_refresh").trigger("click");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));
    }
}